package com.novoda.runbuddy.activity;

import java.util.LinkedList;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.Spinner;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.CompoundButton.OnCheckedChangeListener;

import com.novoda.runbuddy.R;
import com.novoda.runbuddy.controller.OptionsMenuImpl;
import com.novoda.runbuddy.model.Run;
import com.novoda.runbuddy.model.ListOfRuns;
import com.novoda.runbuddy.service.CannedRunManagerImpl;
import com.novoda.runbuddy.service.RemoteStopClockServiceInterface;

public class DebugSettings extends Activity {
	private OptionsMenuImpl	optionsMenu;

	@Override
	public void onCreate(Bundle icicle) {
		super.onCreate(icicle);
		optionsMenu = new OptionsMenuImpl(this);
		
		setContentView(R.layout.debugsettings);

		createCannedRoutesOnOffCheckbox();
		createCannedRoutesSpinner();
		createSpeedsSpinner();
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		optionsMenu.addAllActivitiesToMenu(menu);
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		optionsMenu.selectItem(item);
		return super.onOptionsItemSelected(item);
	}

	@Override
	protected void onStart() {
		super.onStart();
		RemoteStopClockServiceInterface.getInstance().registerListener(this);
	}

	@Override
	protected void onStop() {
		super.onStop();
		RemoteStopClockServiceInterface.getInstance().unregisterListener(this);
	}

	@Override
	protected void onResume() {
		super.onResume();

		CheckBox cannedRoutesAreOnCheckbox = (CheckBox) findViewById(R.id.cannedChk);

		if (RemoteStopClockServiceInterface.getInstance().isClockRunning()) {
			cannedRoutesAreOnCheckbox.setEnabled(false);
		} else {
			cannedRoutesAreOnCheckbox.setEnabled(true);
		}
		enableControls(cannedRoutesAreOnCheckbox.isChecked());

	}

	private void enableControls(boolean enabled) {
		Spinner routeSpinner = (Spinner) findViewById(R.id.routeSpinner);
		Spinner speedSpinner = (Spinner) findViewById(R.id.speedSpinner);

		if (enabled) {
			speedSpinner.setEnabled(true);
		} else {
			speedSpinner.setEnabled(false);
		}

		if (enabled && !RemoteStopClockServiceInterface.getInstance().isClockRunning()) {
			routeSpinner.setEnabled(true);
		} else {
			routeSpinner.setEnabled(false);
		}

		RemoteStopClockServiceInterface.getInstance().setCannedRouteEnabled(enabled);
	}

	private void createCannedRoutesOnOffCheckbox() {
		CheckBox cannedBox = (CheckBox) findViewById(R.id.cannedChk);
		cannedBox.setOnCheckedChangeListener(cannedBoxListener);
		cannedBox.setChecked(RemoteStopClockServiceInterface.getInstance().getCannedRouteEnabled());
		enableControls(cannedBox.isChecked());
	}

	private void createCannedRoutesSpinner() {
		ListOfRuns cannedRuns = CannedRunManagerImpl.readCannedRuns(this);
		Spinner routeSpinner = (Spinner) findViewById(R.id.routeSpinner);

		List<String> routes = new LinkedList<String>();
		for (Run j : cannedRuns) {
			routes.add(j.getName());
		}

		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, routes);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		routeSpinner.setAdapter(adapter);
		routeSpinner.setOnItemSelectedListener(routeSpinnerListener);
		routeSpinner.setSelection(RemoteStopClockServiceInterface.getInstance().getCannedRouteId());
	}

	private void createSpeedsSpinner() {
		List<String> speeds = new LinkedList<String>();
		speeds.add("crawl");
		speeds.add("walk");
		speeds.add("jog");
		speeds.add("run");
		speeds.add("sprint");

		Spinner speedSpinner = (Spinner) findViewById(R.id.speedSpinner);
		speedSpinner.setOnItemSelectedListener(speedSpinnerListener);

		ArrayAdapter<String> speedAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, speeds);
		speedAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		speedSpinner.setAdapter(speedAdapter);
		speedSpinner.setSelection((int) (RemoteStopClockServiceInterface.getInstance().getCannedRouteSpeed() / 2) - 1);
	}

	private OnCheckedChangeListener	cannedBoxListener		= new OnCheckedChangeListener() {
																public void onCheckedChanged(CompoundButton buttonView,
																		boolean isChecked) {
																	enableControls(isChecked);
																}

															};

	private OnItemSelectedListener	routeSpinnerListener	= new OnItemSelectedListener() {
																@SuppressWarnings("unchecked")
																public void onItemSelected(AdapterView parent, View v,
																		int position, long id) {
																	if (!RemoteStopClockServiceInterface.getInstance()
																			.isClockRunning()) {
																		RemoteStopClockServiceInterface.getInstance()
																				.setCannedRouteId(position);
																	}
																}

																@SuppressWarnings("unchecked")
																public void onNothingSelected(AdapterView parent) {
																	if (!RemoteStopClockServiceInterface.getInstance()
																			.isClockRunning()) {
																		RemoteStopClockServiceInterface.getInstance()
																				.setCannedRouteId(1);
																	}
																}

															};

	private OnItemSelectedListener	speedSpinnerListener	= new OnItemSelectedListener() {
																@SuppressWarnings("unchecked")
																public void onItemSelected(AdapterView parent, View v,
																		int position, long id) {
																	RemoteStopClockServiceInterface.getInstance()
																			.setCannedRouteSpeed(2.0 * (position + 1));
																}

																@SuppressWarnings("unchecked")
																public void onNothingSelected(AdapterView parent) {
																	RemoteStopClockServiceInterface.getInstance()
																			.setCannedRouteSpeed(1.0);
																}

															};

}
